package com.ambow.shop.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.shop.action.Action;
import com.ambow.shop.action.impl.DefaultAction;
import com.ambow.shop.domain.Product;
import com.ambow.shop.domain.ShoppingBasket;
import com.ambow.shop.domain.ShoppingBasketItem;
import com.ambow.shop.service.ProductService;
import com.ambow.shop.service.impl.ProductServiceImpl;

/**
 * 购物车控制器。
 * 
 * @author deng.zhao
 *
 */
@WebServlet(name = "ShoppingBasketController", urlPatterns = "/shoppingbasket/*")
public class ShoppingBasketController extends HttpServlet {
	private ProductService productService = new ProductServiceImpl();
	private static final long serialVersionUID = 1L;

	public ShoppingBasketController() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		process(request, response);
	}

	private String getActionName(String uri) {
		return uri.substring(uri.lastIndexOf("/") + 1);
	}

	private void process(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String actionName = getActionName(request.getRequestURI());
		ShoppingBasket basket = (ShoppingBasket) request.getSession().getAttribute("basket");
		List<ShoppingBasketItem> items = basket.getItems();
		if ("additem".equals(actionName)) {
			Integer id = Integer.parseInt(request.getParameter("productId"));

			// 判断当前添加的产品在购物车中是否存在；
			boolean hasOne = false;
			ShoppingBasketItem item = null;
			for (ShoppingBasketItem shoppingBasketItem : items) {
				if (shoppingBasketItem.getProduct().getId() == id) {
					hasOne = true;
					item = shoppingBasketItem;
					break;
				}
			}
			if (hasOne) {
				// 修改；
				item.setQuantity(item.getQuantity() + 1);
			} else {
				// 新增；
				Product product = productService.getProduct(id);
				item = new ShoppingBasketItem(product, 1, basket);
				items.add(item);
			}

		} else if ("detail".equals(actionName)) {

		} else if ("removeitem".equals(actionName)) {
			Integer id = Integer.parseInt(request.getParameter("productId"));
			for (int i = 0; i < items.size(); i++) {
				if (items.get(i).getProduct().getId() == id) {
					items.remove(i);
					break;
				}
			}
		} else if ("updateitem".equals(actionName)) {
			Integer id = Integer.parseInt(request.getParameter("productId"));
			Integer quantity = Integer.parseInt(request.getParameter("quantity"));
			for (int i = 0; i < items.size(); i++) {
				ShoppingBasketItem item = items.get(i);
				if (item.getProduct().getId() == id) {
					item.setQuantity(quantity);
					break;
				}
			}
		} else {

		}

		request.getRequestDispatcher("../view/shoppingBasketDetail.jsp").forward(request, response);
	}
}
